package com.xiaohudie.JVM;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author xiao hu die
 * @Date 2022/3/22 16:55
 * @Version 1.0
 */
public class RuanYinYong {
    public static void main(String[] args) {
        /**强引用指向弱应用*/
        List<SoftReference<byte[]>> list =    new ArrayList<>();
        /**新建引用队列*/
        ReferenceQueue<byte[]> objectReferenceQueue = new ReferenceQueue<>();

        for (int i = 0; i <5 ; i++) {
            SoftReference<byte[]> ref =new SoftReference<>(new byte[4*1024*1024],objectReferenceQueue);
            System.out.println(ref.get());
            list.add(ref);
            System.out.println(list.size());
        }
        Reference<? extends byte[]> poll = objectReferenceQueue.poll();
        while (poll !=null){
            list.remove(poll);
            poll =objectReferenceQueue.poll();
        }
        System.out.println("循环结束:"+list.size());
        for (SoftReference<byte[]> softReference : list) {
            System.out.println(softReference.get());
        }
    }
}
